//
//  AddCreditCardController.m
//  MyBank
//
//  Created by 崔宝印 on 2017/7/26.
//  Copyright © 2017年 Koolearn. All rights reserved.
//

#import "AddCreditCardController.h"
#import "CoreDataHelper.h"

@interface AddCreditCardController ()

@property (weak, nonatomic) IBOutlet UITextField *tfBank;
@property (weak, nonatomic) IBOutlet UITextField *tfCardNo;
@property (weak, nonatomic) IBOutlet UITextField *tfValidate;
@property (weak, nonatomic) IBOutlet UITextField *tfPinNo;
@property (weak, nonatomic) IBOutlet UITextField *tfBillDay;
@property (weak, nonatomic) IBOutlet UITextField *tfLimitDay;
@property (weak, nonatomic) IBOutlet UITextField *tfCheckPsd;
@property (weak, nonatomic) IBOutlet UITextField *tfTradePsd;
@property (weak, nonatomic) IBOutlet UITextField *tfTotalLimit;
@property (weak, nonatomic) IBOutlet UITextField *tfTempLimit;
@property (weak, nonatomic) IBOutlet UITextField *tfAvilibleLimit;
@property (weak, nonatomic) IBOutlet UITextField *tfDebtLimit;

@end

@implementation AddCreditCardController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(bankSelectHandler:) name:@"BankSelectFinished" object:nil];
}

#pragma mark - NSNotification
- (void)bankSelectHandler:(NSNotification *)notification
{
    self.tfBank.text = notification.object;
}

#pragma mark - IBAction
- (IBAction)goBackAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)saveCardAction:(id)sender
{
    CoreDataHelper *coreData = [CoreDataHelper sharedInstance];
    NSEntityDescription *objectDes = [NSEntityDescription entityForName:@"CreditCard" inManagedObjectContext:coreData.managedObjectContext];
    CreditCard *cardModel = (CreditCard *)[[NSManagedObject alloc] initWithEntity:objectDes insertIntoManagedObjectContext:coreData.managedObjectContext];
    cardModel.bankName = self.tfBank.text;
    cardModel.cardNo = self.tfCardNo.text;
    cardModel.billDay = self.tfBillDay.text;
    cardModel.pinNo = self.tfPinNo.text;
    cardModel.tradePsd = self.tfTradePsd.text;
    cardModel.checkPsd = self.tfCheckPsd.text;
    cardModel.expDate = self.tfValidate.text;
    cardModel.totalLimit = [self.tfTotalLimit.text floatValue];
    cardModel.temporaryLimit = [self.tfTempLimit.text floatValue];
    cardModel.availableLimit = [self.tfAvilibleLimit.text floatValue];
    cardModel.repaymentDay = self.tfDebtLimit.text;
    [coreData saveContext];
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 6;
    }
    else
    {
        return 6;
    }
}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
